<?php

namespace app\modules\Order\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\Order\models\Leader;

/**
 * LeaderSearch represents the model behind the search form about `app\modules\Order\models\Leader`.
 */
class LeaderSearch extends Leader
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'gender', 'passport_type', 'status', 'user_id'], 'integer'],
            [['first_name', 'last_name', 'en_first_name', 'en_last_name', 'code', 'pinyin', 'birthday', 'country', 'passport', 'passport_add', 'passport_date', 'phone', 'remark', 'created_at', 'updated_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Leader::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'gender' => $this->gender,
            'birthday' => $this->birthday,
            'passport_date' => $this->passport_date,
            'passport_type' => $this->passport_type,
            'status' => $this->status,
            'user_id' => $this->user_id,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'first_name', $this->first_name])
            ->andFilterWhere(['like', 'last_name', $this->last_name])
            ->andFilterWhere(['like', 'en_first_name', $this->en_first_name])
            ->andFilterWhere(['like', 'en_last_name', $this->en_last_name])
            ->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'pinyin', $this->pinyin])
            ->andFilterWhere(['like', 'country', $this->country])
            ->andFilterWhere(['like', 'passport', $this->passport])
            ->andFilterWhere(['like', 'passport_add', $this->passport_add])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'remark', $this->remark]);

        return $dataProvider;
    }
}
